﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany
{
    public partial class raw_ctrl : UserControl
    {
        private eCompany.eObj.company m_comp = null;
        private eCompany.eObj.material_type_enum m_rawType;
        private eCompany.eObj.currency_list m_cl = null;
        private string m_currency = null;

        public raw_ctrl(eCompany.eObj.company comp, string currency, eCompany.eObj.material_type_enum rawType, eCompany.eObj.currency_list cl)
        {
            InitializeComponent();

            m_comp = comp;
            m_rawType = rawType;
            m_cl = cl;
            m_currency = currency;
            RefreshRawList();
            RefreshTotalRawValue();
        }

        private void RefreshRawList()
        {
            eCompany.eManager.eManager eM = new eManager.eManager();
            rawListView.Items.Clear();
            eCompany.eObj.material_stock ms = eM.getMaterialStock(m_comp, m_rawType);

            foreach (eCompany.eObj.material m in ms.materials)
            {
                ListViewItem item = new ListViewItem();
                item.Text = m.date.ToString();
                item.SubItems.Add(m.volume_in.ToString());
                item.SubItems.Add(m.volume_out.ToString());
                item.SubItems.Add(eCompany.eManager.eManager.formatDouble(m.unit_price)+m_currency);
                item.SubItems.Add(eCompany.eManager.eManager.formatDouble(m.volume_in * m.unit_price)+m_currency);
                if (m.volume_out < m.volume_in)
                    item.Font = new Font(item.Font.FontFamily, item.Font.Size, FontStyle.Bold);

                rawListView.Items.Add(item);
            }
        }

        private void RefreshTotalRawValue()
        {
            eCompany.eManager.eManager eM = new eManager.eManager();
            double shop_local_value = eM.getMaterialCount(m_comp,m_rawType);
            /*
            lblShopLocalValue.Text = eCompany.eManager.eManager.formatCurrency(shop_local_value) + s.currency;
            lblShopGoldValue.Text = eCompany.eManager.eManager.formatCurrency(eM.currencyToGOLD(m_cl, s.currency, shop_local_value)) + "GLD";
             */
        }

        private void buyCmd_Click(object sender, EventArgs e)
        {
            string str_units = "";
            string str_price = "";

            if (Tools.InputBox("Buy Raw Material", "Number of units", "Unit price", ref str_units, ref str_price) == DialogResult.OK)
            {
                System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
                str_units = str_units.Replace(",", ".");
                int units = Convert.ToInt32(str_units, culture);
                str_price = str_price.Replace(",", ".");
                double price = double.Parse(str_price, System.Globalization.NumberStyles.Number,culture);

                eCompany.eManager.eManager eM = new eManager.eManager();
                eM.buyMaterials(m_comp, m_rawType, units, price);
                RefreshRawList();
                RefreshTotalRawValue();
            }
        }
    }
}
